//
// Created by liang on 2025/4/6.
//

#ifndef QT6_TEST_WINDOWCONTROL_H
#define QT6_TEST_WINDOWCONTROL_H
#include <windows.h>
#include <AutoItX3_DLL.h>
#include <codecvt>
#include <locale>
#include <map>
#include <QCoreApplication>
#include <vector>
#include <QString>

#include "ActionKey.h"
#include "SSPoint.h"
#include "TaskWindow.h"

namespace SS {
    class WindowControl {
    private:
        using AU3_WinGetPosFunc = int(__stdcall* )(const char*, const char*, int*, int*, int*, int*);
        using AutoItInitFunc = int(__stdcall* )();
        using AutoItExitFunc = int(__stdcall* )();
        using ActivateByHandle = int(__stdcall*)(HWND hWnd);
        using AutoItX_MouseClick = int (__stdcall *)(const wchar_t* button, int x, int y, int clicks, int speed);
        using AutoItX_Send = int (__stdcall *)(const wchar_t* keys, int nMode);
    public:
        explicit WindowControl() ;
        ~WindowControl();

        const std::map<HWND, SS::TaskWindow>& getAllWindows();
        SS::Point getWindowPosition(HWND hwnd) {

        }
        void activeWindowByHwnd(HWND hwnd) {
            activateByHandle(hwnd);
        };
        void leftMouseClick(int x, int y) {
            mouse_click(L"left", x, y, 1, 10);
        };
        void rightMouseClick(int x, int y) {
            mouse_click(L"right", x, y, 1, 10);
        };
        void sendActionKey(HWND hwnd, ActionKey::Keys key) {
            activateByHandle(hwnd);
            auto keyStr = ActionKey::getKeyString(key);
            sendKey(keyStr.toStdWString().c_str(),0);
        }
    private:
        AutoItInitFunc autoItInit;
        AutoItExitFunc autoItExit;
        AU3_WinGetPosFunc au3WinGetPos;
        ActivateByHandle activateByHandle;
        AutoItX_MouseClick mouse_click;
        AutoItX_Send sendKey;
        HINSTANCE hDll;
        std::map<HWND,SS::TaskWindow> windowMap;
        void initHDll() {
            QString dllPath = QCoreApplication::applicationDirPath() + "/third_part/TaskWindow.dll";
            hDll = LoadLibrary(dllPath.toStdWString().c_str());
            autoItInit = reinterpret_cast<AutoItInitFunc>(GetProcAddress(hDll, "AutoItInit"));
            autoItExit = reinterpret_cast<AutoItExitFunc>(GetProcAddress(hDll, "AutoItExit"));
            au3WinGetPos = reinterpret_cast<AU3_WinGetPosFunc>(GetProcAddress(hDll, "AU3_WinGetPos"));
            activateByHandle = reinterpret_cast<ActivateByHandle>(GetProcAddress(hDll, "AU3_WinActivateByHandle"));
            mouse_click = reinterpret_cast<AutoItX_MouseClick>(GetProcAddress(hDll, "AU3_MouseClick"));
            sendKey = reinterpret_cast<AutoItX_Send>(GetProcAddress(hDll, "Send"));
        }
    };

}



#endif //QT6_TEST_WINDOWCONTROL_H
